package com.example.sysao.controller;

import com.example.sysao.pejo.Repair;
import com.example.sysao.pejo.Student;
import com.example.sysao.pejo.Urgent;
import com.example.sysao.service.ReapirService;
import com.example.sysao.service.UrgentService;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Component
@RequestMapping("/urgent")
public class UrgentController {
    @Resource
    private UrgentService urgentService;
    @Resource
    private ReapirService reapirService;

    @PostMapping("/addUrgent")
    public Map<String, Object> addReminder(int id, HttpSession session) {
        Urgent urgent = new Urgent();
        LocalDateTime time = LocalDateTime.now();
        Student student = (Student) session.getAttribute("student");
        urgent.setCreateTime(time);
        urgent.setStatus(1);
        urgent.setRepairId(id);

        //更改维修单的状态为3
//        Repair repair = reapirService.findById(id);
//        repair.setStatus(3);
//        reapirService.saveRepair(repair);
//
        urgent.setStudentId(student.getId());
        Map<String, Object> map = new HashMap<>();
        if (urgentService.findByRapirId(id) == 0 && urgentService.addUrgent(urgent)) {
            map.put("success", true);
        }
        return map;
    }


    @RequestMapping("/list")
    public List<Urgent> findAll() {
        return urgentService.findAll();
    }

    @DeleteMapping("/deleteUrgent/{Id}")
    public Map<String, Object> deleteuser(@PathVariable("Id") int id) {
        Map<String, Object> map = new HashMap<>();
        map.put("success", false);
        if (urgentService.removeUrgent(id)) {
            map.put("success", true);
        }
        return map;
    }

    //同意取消删除维修单
    @DeleteMapping("/deleteRepair/{Id}")
    public Map<String, Object> deleteRepair(@PathVariable("Id") int id) {
        Map<String, Object> map = new HashMap<>();
        map.put("success", false);
        if (reapirService.removeRepair(id)) {
            map.put("success", true);
        }
        return map;
    }

    @PostMapping("/updateUrgent")
    public Map<String, Object> saveurgent(int id) {
        Map<String, Object> map = new HashMap<>();
        LocalDateTime time = LocalDateTime.now();
        System.out.println(id);
        Urgent urgent = urgentService.findById(id);
        urgent.setCreateTime(time);
        urgent.setStatus(1);
        if (urgentService.saveUrgent(urgent)) {
            map.put("success", true);
        }
        return map;
    }

    //更新催单时间和更改催单状态为1
    @RequestMapping("/updateUrgent1")
    public Map<String, Object> saveurgentand(int uid, int rid) {
        Map<String, Object> map = new HashMap<>();
        LocalDateTime time = LocalDateTime.now();
        Urgent urgent = urgentService.findById(uid);
        urgent.setCreateTime(time);
        //改催单状态为1
        Repair repair = reapirService.findById(rid);
        repair.setStatus(1);

        if (urgentService.saveUrgent(urgent) && reapirService.saveRepair(repair)) {
            map.put("success", true);
        }
        return map;
    }

    //admin
    @RequestMapping("check")
    public Map<String, Object> checkUrgent(int uid) {
        Map<String, Object> map = new HashMap<>();
        Urgent urgent = urgentService.findById(uid);
        urgent.setStatus(2);
        if (urgentService.saveUrgent(urgent)) {
            map.put("success", true);
        }
        return map;
    }

//    @RequestMapping("allUrgents")
//    public List<Urgent> allUrgents() {
//        List<Integer> ids = new ArrayList<>();
//        List<Urgent> list = urgentService.findAll();
//        for (int i = 0; i < list.size(); i++) {
//            Urgent urgent = list.get(i);
//            ids.add(urgent.getId());
//        }
//        List<Urgent> repairlist = new ArrayList<>();
//        for (int id : ids) {
//            Urgent urgent1 = urgentService.findById(id);
//            repairlist.add(urgent1);
//        }
//        return repairlist;
//    }

    //根据维修单状态判断是否已经查看催单
    @RequestMapping("allUrgents")
    public List<Urgent> checkUrgents() {
        List<Integer> ids = new ArrayList<>();
        List<Integer> rid = new ArrayList<>();//维修单号集合
        int[] arr = new int[]{};
        List<Urgent> repairlist = new ArrayList<>();//最后返回对象
        List<Urgent> list = urgentService.findAll();


        for (int i = 0; i < list.size(); i++) {
            Urgent urgent = list.get(i);
            ids.add(urgent.getId());

            rid.add(urgent.getRepairId());
        }
        System.out.println(ids);//催单集合
        System.out.println(rid);//维修单号集合

        for (Integer id : rid) {
            Repair repair = reapirService.findById(id);
            if (repair.getStatus() == 2 || repair.getStatus() == 5 || repair.getStatus() == 6) {
                System.out.println("sdfggdasdf");
                Urgent urgent = urgentService.findByRapirid(repair.getId());
                urgent.setStatus(2);
                urgentService.saveUrgent(urgent);
            }
        }
        for (int id : ids) {
            Urgent urgent2 = urgentService.findById(id);
            repairlist.add(urgent2);
        }
        return repairlist;
    }
}




